package com.weibo.common;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.weibo.sql.CommonParm;
import com.weibo.user.User;

/**
 * 功能：工具类
 * @author caixiaolong
 *
 */
public class CommonUtil {
	private static Logger logger = Logger.getLogger(CommonUtil.class);
	/**
	 * 功能：剔除sql中的 单引号特殊字符 并将 参数合并到sql语句中
	 * @param sql sql语句
	 * @param para sql语句中的参数
	 * @return 替换过sql语句中占位符？号之后的sql语句
	 */
	public static String getSqlBy_par(String sql,Object... para){
		String temp = null;
		for(int i=0;i<para.length;i++){
			temp = para[i]==null?"":para[i].toString();
			sql  = sql.replaceFirst("\\?","'"+temp+"'");
		}
		return sql;
	}
	
	/**
	 * 获取分页的sql语句
	 * @param sql 不带分页的sql语句
	 * @param currentPage 当前也
	 * @return 带分页的sql语句
	 */
	public static String getPageSql(String sql,int currentPage){
		
		StringBuffer sql_temp =new StringBuffer();
			sql_temp.append("SELECT");
			sql_temp.append("*");
			sql_temp.append("FROM ");
			sql_temp.append("( ");
			sql_temp.append("SELECT ");
			sql_temp.append(" * ");
			sql_temp.append("FROM ");
			sql_temp.append("( ");
			sql_temp.append("SELECT ");
			sql_temp.append("T .*, ROW_NUMBER () OVER (ORDER BY NULL) AS row_number ");
			sql_temp.append("FROM (");
			sql_temp.append(sql);
			sql_temp.append(" )T ");
			sql_temp.append(") P ");
			sql_temp.append("WHERE ");
			sql_temp.append("P .row_number > ");
			sql_temp.append(currentPage*CommonParm.PAGESIZE);
			sql_temp.append(") q ");
			sql_temp.append("WHERE ");
			sql_temp.append("ROWNUM <=");
			sql_temp.append(CommonParm.PAGESIZE);
		return sql_temp.toString();
	}
	
	/**
	 * 功能：将ResultSet 装换成List
	 * @param rs ResultSet结果集
	 * @return 分装后的List结果集
	 * @throws SQLException
	 */
	public static List convertList(ResultSet rs) throws SQLException {
		List<Map> list = new ArrayList<Map>();
		Map<String, Object> rowData = null;
		ResultSetMetaData md = rs.getMetaData();
		int columnCount = md.getColumnCount(); //Map rowData;
		while (rs.next()) { //rowData = new HashMap(columnCount);
			rowData = new HashMap<String, Object>();
			for (int i = 1; i <= columnCount; i++) {
				rowData.put(md.getColumnName(i), rs.getObject(i));
			}
				list.add(rowData);
		} return list;
	}
	
	/**
	 * 功能：根据总记录条数获取总页数
	 * @param TotalNum
	 * @return
	 */
	public static int getTotalPageNum(int TotalNum){
		if(TotalNum==0){
			return TotalNum;
		}else{
			if(TotalNum/CommonParm.PAGESIZE==0)
				return TotalNum/CommonParm.PAGESIZE;
			else
				return TotalNum/CommonParm.PAGESIZE +1;
		}
		
	}
	
	public static List getUserList_ByRs(ResultSet rs){
		List<User> list = new ArrayList<User>();
		User user = null;
		try {
			while(rs.next()){ 
				user = new User();
				user.setUser_city(rs.getString("user_city")==null?"":rs.getString("user_city"));
				user.setUser_comppany(rs.getString("user_comppany")==null?"":rs.getString("user_comppany"));
				user.setUser_duty(rs.getString("user_duty")==null?"":rs.getString("user_duty"));
				user.setUser_duty_sort(rs.getString("user_duty_sort")==null?"":rs.getString("user_duty_sort"));
				user.setUser_exit_time(rs.getString("user_exit_time")==null?"":rs.getString("user_exit_time"));
				user.setUser_freeze(rs.getString("user_freeze")==null?"":rs.getString("user_freeze"));
				user.setUser_id(rs.getString("user_id"));
				user.setUser_mail(rs.getString("user_mail")==null?"":rs.getString("user_mail"));
				user.setUser_name(rs.getString("user_name")==null?"":rs.getString("user_name"));
				user.setUser_order_sort(rs.getString("user_order_sort")==null?"":rs.getString("user_order_sort"));
				user.setUser_order_time(rs.getString("user_order_time")==null?"":rs.getString("user_order_time"));
				user.setUser_phone(rs.getString("user_phone")==null?"":rs.getString("user_phone"));
				user.setUser_psw(rs.getString("user_psw")==null?"":rs.getString("user_psw"));
				user.setUser_rename(rs.getString("user_rename")==null?"":rs.getString("user_rename"));
				user.setUser_sex(rs.getString("user_sex")==null?"":rs.getString("user_sex"));
				user.setUser_state(rs.getString("user_state")==null?"":rs.getString("user_state"));
				user.setUser_telphone(rs.getString("user_telphone")==null?"":rs.getString("user_telphone"));
				user.setUser_type(rs.getString("user_type")==null?"":rs.getString("user_type"));
				user.setUser_weibo_path(rs.getString("user_weibo_path")==null?"":rs.getString("user_weibo_path"));
				user.setUser_yue(rs.getString("user_yue")==null?"":rs.getString("user_yue"));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		return list;
	}

	/**
	 * 获取已经登陆的用户
	 * @param request
	 * @return
	 */
	public static User getLoginUser(HttpServletRequest request){
		User user = null;
		Object obj = request.getSession().getAttribute("login_user");
		if(obj!=null){
			user = (User)obj;
		}
		return user;
	}
	/**
	 * 字符编码转换
	 * @param obj
	 * @return
	 */
	public static String parseUrlZH(Object obj){
		if(obj!=null){
			try {
				return new String(obj.toString().getBytes("ISO-8859-1"),"UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				return "";
			}
		}else{
			return "";
		}
	}
	
	public static String getTotalSizeSql_bySql(String sql){
		return "SELECT COUNT(*) NUM FROM ( "+sql+" )OTHERTABLE";
	} 
	public static void main(String args[]){
		String str= "select * from user where id like ? and name like ?";
		 //System.out.println(str);
		 String s = "\\?";
		 //str=str.replaceAll(s,"");
		 
		 String temp = str.replaceFirst(s, "3333"); 
		 System.out.println(temp);
	}

}
